<template>
  <li 
    :class="{ completed: todo.completed }"
  >
    <input 
      type="checkbox" 
      v-model="completed"
      @change="updateCompleted"
      class="todo-checkbox"
    />
    <span class="todo-text">{{ todo.text }}</span>
    <button @click="deleteTodo" class="delete-btn">删除</button>
  </li>
</template>

<script setup>
import { defineProps, defineEmits, computed } from 'vue';

const props = defineProps({
  todo: {
    type: Object,
    required: true
  }
});

const emit = defineEmits(['update:completed', 'delete']);

const completed = computed({
  get: () => props.todo.completed,
  set: (value) => emit('update:completed', { ...props.todo, completed: value })
});

const updateCompleted = () => {
  emit('update:completed', { ...props.todo, completed: !props.todo.completed });
};

const deleteTodo = () => {
  emit('delete', props.todo.id);
};
</script>

<style scoped>
.todo-list li {
  display: flex;
  align-items: center;
  padding: 10px;
  margin-bottom: 8px;
  background-color: #f9f9f9;
  border-radius: 4px;
}

.todo-checkbox {
  margin-right: 10px;
}

.todo-text {
  flex: 1;
}

.completed .todo-text {
  text-decoration: line-through;
  color: #888;
}

.delete-btn {
  padding: 5px 10px;
  background-color: #ff6b6b;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-left: 10px;
}

.delete-btn:hover {
  background-color: #ff5252;
}
</style>